package org.hgvc.webapp.ets.jsf.beans.partner;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.Partner;
import org.hgvc.webapp.ets.bo.State;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class PartnerProfileBean extends AbstractBean {

	public static final String NAV_VIEW_ACTION="partnerUpdateProfile";
	public static final String BEAN_NAME="partnerProfileBean";

	
	private MasterSetupService setupService;
	private PartnerService service;
	private Partner partner;
	private String compCountryId;
	private String compStateId;
	private String mailCountryId;
	private String mailStateId;
	private List<SelectItem> countryList;
	private List<SelectItem> compStateList;
	private List<SelectItem> mailStateList;
	private List<SelectItem> mailSelectList;
	private String mailBy = CommonConstant.MAIL_SELECT_OTHER;
	private boolean checkFlag = false;
	private String regNo;
	private String login;
	private String password;
	

	public void setSetupService(MasterSetupService setupService) {
		this.setupService = setupService;
	}
	public void setService(PartnerService service) {
		this.service = service;
	}
	public Partner getPartner() {
		return partner;
	}
	public void setPartner(Partner partner) {
		this.partner = partner;
	}
	public String getCompCountryId() {
		return compCountryId;
	}
	public void setCompCountryId(String compCountryId) {
		this.compCountryId = compCountryId;
	}
	public String getCompStateId() {
		return compStateId;
	}
	public void setCompStateId(String compStateId) {
		this.compStateId = compStateId;
	}
	public String getMailCountryId() {
		return mailCountryId;
	}
	public void setMailCountryId(String mailCountryId) {
		this.mailCountryId = mailCountryId;
	}
	public String getMailStateId() {
		return mailStateId;
	}
	public void setMailStateId(String mailStateId) {
		this.mailStateId = mailStateId;
	}
	public void setCompStateList(List<SelectItem> compStateList) {
		this.compStateList = compStateList;
	}
	public String getMailBy() {
		return mailBy;
	}
	public boolean isCheckFlag() {
		return checkFlag;
	}
	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}
	public String getRegNo() {
		return regNo;
	}
	public void setRegNo(String regNo) {
		this.regNo = regNo;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	//-----------------------------------------------------------------------------
	public String navVewAction(Partner partner){
		
		this.partner = partner;
		mailBy=CommonConstant.MAIL_SELECT_COMP;
			checkFlag = false;
			
			if(partner.getCompCountry()!=null){
				compCountryId = partner.getCompCountry().getCountryId();
			}else{
				compCountryId = null;
			}
			if(partner.getCompState()!=null){
				compStateId = partner.getCompState().getStateId();
			}else{
				compStateId = null;
			}
			
			if(partner.getMailCountry()!=null){
				mailCountryId = partner.getMailCountry().getCountryId();
			}else{
				mailCountryId = null;
			}
			if(partner.getMailState()!=null){
				mailStateId = partner.getMailState().getStateId();
			}else{
				mailStateId = null;
			}
			
		return NAV_VIEW_ACTION;
	}
	
	public List<SelectItem> getCountryList() {
		try{
			countryList = new ArrayList<SelectItem>();
			List<Country> cList = setupService.getCountryList();
			for(Country country:cList){
				SelectItem item = new SelectItem(country.getCountryId(),country.getCode()+" "+country.getDesc());
				countryList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return countryList;
	}
	public List<SelectItem> getMailStateList() {
		try{
			mailStateList = new ArrayList<SelectItem>();
			if(mailCountryId!=null&&mailCountryId.length()>0){
				List<State> cList = setupService.getStateList(mailCountryId);
				for(State state:cList){
					SelectItem item = new SelectItem(state.getStateId(),state.getCode()+" "+state.getDesc());
					mailStateList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return mailStateList;
	}
	public List<SelectItem> getCompStateList() {
		try{
			compStateList = new ArrayList<SelectItem>();
			if(compCountryId!=null&&compCountryId.length()>0){
				List<State> cList = setupService.getStateList(compCountryId);
				for(State state:cList){
					SelectItem item = new SelectItem(state.getStateId(),state.getCode()+" "+state.getDesc());
					compStateList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return compStateList;
	}

	
	public List<SelectItem> getMailSelectList() {

		mailSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.MAIL_COMP_OTHER_SELECT_LIST){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			mailSelectList.add(item);
		}

		return mailSelectList;
	}
	
	public void setMailBy(String mailBy) {
		this.mailBy = mailBy;
		if(mailBy.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
			checkFlag = false;
		}else{
			checkFlag = true;
		}
	}
	
	public String updatePartnerProfileAction(){
		try{
			
			partner.setCompCountry(new Country(compCountryId));
			partner.setCompState(new State(compStateId));
			if(!checkFlag){
				partner.setMailAddress1(partner.getCompAddress1());
				partner.setMailAddress2(partner.getCompAddress2());
				partner.setMailCity(partner.getCompCity());
				partner.setMailPostcode(partner.getCompPostcode());
				partner.setMailCountry(partner.getCompCountry());
				partner.setMailState(partner.getCompState());
			}else{
				partner.setMailCountry(new Country(mailCountryId));
				partner.setMailState(new State(mailStateId));
			}
			service.updatePartnerProfile(partner, getThisUser());
			return PartnerBean.NAV_PARTNER_PORTAL;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_VIEW_ACTION;
	}
	
	public String backAction(){
		return PartnerBean.NAV_PARTNER_PORTAL;
	}
}
